#include "Parser.h"

ObjectModel *CreateNewObject(int X, int Y)
{
	FileParser Parser;

	Parser.NewFile( (std::string("ObjectData/Adjustable_Object_") + 
                   std::string( ToString( gCurrentFile ) +
                   std::string(".txt") )).c_str() );

	int result = Parser.ParseFile();

  if( result != 1 )
    return NULL;

	DataNode *Data = Parser.GetData();

	std::map<std::string, float>::iterator DataWalker = Data->DataParameters.begin();
	std::map<std::string, float>::iterator DataEnd = Data->DataParameters.end();
	ObjectModel *RetVal = new ObjectModel(Data->Texture,NewVector((float)X,(float)Y),NewVector(1.f,1.f));
  RetVal->brain.SetName( Data->Name );

	while (DataWalker != DataEnd)
	{
		RetVal->brain.AddStimulus(DataWalker->first,DataWalker->second,GenericStimOperation);
		++DataWalker;
	}

	return RetVal;
}

AgentModel *CreateNewAgent(int X, int Y)
{
	FileParser Parser;

	Parser.NewFile( (std::string("AgentData/Adjustable_Agent_") + 
                   std::string( ToString( gCurrentFile ) +
                   std::string(".txt") )).c_str() );

	int result = Parser.ParseFile();

  if( result != 1 )
    return NULL;

	DataNode *Data = Parser.GetData();

	std::map<std::string, float>::iterator DataWalker = Data->DataParameters.begin();
	std::map<std::string, float>::iterator DataEnd = Data->DataParameters.end();
	AgentModel *RetVal = new AgentModel(Data->Texture,NewVector((float)X,(float)Y),NewVector(1.f,1.f),.05f);
  RetVal->brain.SetName( Data->Name );

	while (DataWalker != DataEnd)
	{
		RetVal->brain.AddReceptor(DataWalker->first,DataWalker->second,GenericStimRecOperation,GenericRecOperation);
		++DataWalker;
	}

	return RetVal;
}


FileParser::FileParser()
{
	memset(this->FileName, 0, MAX_FILE_NAME_SIZE);
}

FileParser::FileParser(const char *FileName)
{
	strcpy_s(this->FileName, MAX_FILE_NAME_SIZE, FileName);
}

FileParser::~FileParser() {}

void FileParser::NewFile(const char *FileName)
{
	strcpy_s(this->FileName, MAX_FILE_NAME_SIZE, FileName);
}

int FileParser::ParseFile(void)
{
	if (this->FileName[0] == 0)
	{
		return 0;
	}

	char TempBuffer[DATA_BUFFER_SIZE];
	char VolatileData[DATA_BUFFER_SIZE];
	std::string StringBuffer;
	std::ifstream FileStream(FileName);

	if (!FileStream.good())
	{
		return -1;
	}

	// Take the first line of the file, this should hold the name of the object
	FileStream >> TempBuffer;
	strcpy_s(Data.Name, strlen(TempBuffer) + 1, TempBuffer);
	FileStream >> TempBuffer;
	strcpy_s(Data.Texture, strlen(TempBuffer) + 1, TempBuffer);

	// Parse the rest of the file, getting data names and values
	while (!FileStream.eof())
	{
		// Grab the next line from the file, two parts, TempBuffer holds the data name and VolatileData holds the value
		FileStream >> StringBuffer; // Silly standard strings :(
		FileStream >> VolatileData;

		Data.DataParameters[ StringBuffer ] = (float)atof(VolatileData);
	}

	//Data.push_back(TempNode);
	FileStream.close();
	//}

	//MainFile.close();
	
	return 1;
}
